package com.sjgtw.cloud.web.path.service.impl;

import com.sjgtw.cloud.common.core.constant.CacheConstants;
import com.sjgtw.cloud.common.core.utils.DateUtils;
import com.sjgtw.cloud.common.core.utils.SecurityUtils;
import com.sjgtw.cloud.common.redis.service.RedisService;
import com.sjgtw.cloud.system.api.model.LoginUser;
import com.sjgtw.cloud.web.path.domain.PathPoint;
import com.sjgtw.cloud.web.path.mapper.PathPointMapper;
import com.sjgtw.cloud.web.path.service.IPathPointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 点位Service业务层处理
 * 
 * @author lvyoufang
 * @date 2024-03-19
 */
@Service
public class PathPointServiceImpl implements IPathPointService 
{
    @Autowired
    private PathPointMapper pathPointMapper;

    @Autowired
    private RedisService redisService;

    /**
     * 查询点位
     * 
     * @param id 点位主键
     * @return 点位
     */
    @Override
    public PathPoint selectPathPointById(Long id)
    {
        return pathPointMapper.selectPathPointById(id);
    }

    /**
     * 查询点位列表
     * 
     * @param pathPoint 点位
     * @return 点位
     */
    @Override
    public List<PathPoint> selectPathPointList(PathPoint pathPoint)
    {
        return pathPointMapper.selectPathPointList(pathPoint);
    }

    /**
     * 新增点位
     * 
     * @param pathPoint 点位
     * @return 结果
     */
    @Override
    public int insertPathPoint(PathPoint pathPoint)
    {
        String token = SecurityUtils.getToken();
        String userKey = CacheConstants.LOGIN_TOKEN_KEY + token;
        LoginUser user = redisService.getCacheObject(userKey);

        pathPoint.setCreateBy(user.getUsername());
        pathPoint.setCreateTime(DateUtils.getNowDate());
        return pathPointMapper.insertPathPoint(pathPoint);
    }

    /**
     * 修改点位
     * 
     * @param pathPoint 点位
     * @return 结果
     */
    @Override
    public int updatePathPoint(PathPoint pathPoint)
    {
        String token = SecurityUtils.getToken();
        String userKey = CacheConstants.LOGIN_TOKEN_KEY + token;
        LoginUser user = redisService.getCacheObject(userKey);

        pathPoint.setUpdateBy(user.getUsername());
        pathPoint.setUpdateTime(DateUtils.getNowDate());
        return pathPointMapper.updatePathPoint(pathPoint);
    }

    /**
     * 批量删除点位
     * 
     * @param ids 需要删除的点位主键
     * @return 结果
     */
    @Override
    public int deletePathPointByIds(String[] ids)
    {
        return pathPointMapper.deletePathPointByIds(ids);
    }

    /**
     * 删除点位信息
     * 
     * @param id 点位主键
     * @return 结果
     */
    @Override
    public int deletePathPointById(Long id)
    {
        return pathPointMapper.deletePathPointById(id);
    }

    @Override
    public void deleteByPathId(Long pathId) {
        pathPointMapper.deleteByPathId(pathId);
    }

    @Override
    public List<PathPoint> findByUpperIds(List<Long> upperIds) {
        return pathPointMapper.findByUpperIds(upperIds);
    }
}
